#include<iostream>
#include<string>
#include<fstream>
#include<thread>
#include<mutex>

using namespace std;


class Logs
{
    public:
        Logs() : user_name("user"), log_name("Logs.txt"), logs("no logs"), times(0){}
        ~Logs(){}
        void setUser_name(string user_name){this->user_name = user_name;}
        string getUser_name(){return user_name;}
        void setLog_name(string log_name){this->log_name = log_name;}
        string getLog_name(){return log_name;}
        void setLogs(string logs){this->logs = logs;}
        string getLogs(){return logs;}
        void setTime(time_t times){this->times = times;}
        time_t getTimes(){return times;}
    private:
        string user_name;//日志操作人
        string log_name;//日志文件名称
        string logs;//日志内容
        int times;//日志写入时间
};

mutex g_mutex;
Logs a;
string user_name = "wh";
string log_name = "logs.txt";
string logs = "hello world";
int times = 2023;

void write_log()
{
    //上锁
    lock_guard<mutex> lock(g_mutex);
    ofstream ofs;
    
//  a.setLog_name(log_name);
    ofs.open(a.getLog_name(),ios::app);

    a.setUser_name(user_name);
    a.setLogs(logs);
    a.setTime(times);

    ofs << a.getLogs() << " " << a.getUser_name() << " " << a.getTimes() << endl;

    ofs.close();
}

int main()
{
    thread t1(write_log);
    thread t2(write_log);
    t1.join();
    t2.join();
    return 0;
}